import { createApp } from 'vue'
import App from './App.vue'
//引入plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import {router} from "@/router/index.js";
import axios from "axios";
// 引入pinia实现数据状态管理
import { createPinia } from "pinia";
const pinia = createPinia();


// 指定 axios 的全局配置，指定为后端服务器地址
axios.defaults.baseURL = 'http://localhost:8080/'
//指定axios的请求头中携带cookie
axios.defaults.withCredentials = true;

//添加axios请求的拦截器
axios.interceptors.request.use(config => {
    //从本地读取token
    const token = localStorage.getItem('token')
    //判断token是否存在
    if (token) {
        //将token添加到请求头中
        config.headers.Authorization = `Bearer ${token}`
    }
    return config;
});


const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
//定义图片地址全局变量，所有组件都可以使用
app.config.globalProperties.$urlImage = 'http://localhost:8080/images/';

//使用plus
app.use(router)
app.use(ElementPlus)
app.use(pinia)
app.mount('#app')
